<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date: 2010-05-18
 * |Update: 2010-05-24
 * -+-----------------------------------------
 *
 * Hook 钩子管理类
 * @author jinke
 */
class Hook extends Base
{

	public  $hooks = array();
	public  $hook_tags = array();
	private $flag = true;

	/**
	 * 构造函数 获取钩子配置
	 * @access public
	 */
	public function __construct()
	{
		//读取构子配置文件
		$this->flag	= Config::get('hook');
		$this->readHooks();
		Debug::initClass('Hook.class.php');
	}


	/**
	 * 读取钩子配置资料
	 * @access private
	 * @return void
	 */
	private function readHooks()
	{
		$file =SYSTEM_DIR.'hooks.php';
		//检查钩子配置文件是否存在
		if(is_file($file))
		{
			require $file;
			if(isset($hook) && is_array($hook))
				$this->hooks = $hook;
			if(isset($hook_tag) && is_array($hook_tag))
				$this->hook_tags = $hook_tag;
		}
		else
			message_error('Load File','无法加载钩子配置文件！',$file);
	}
	
	
	/**
	 * 启动tag钩子
	 * @param string $tag
	 */
	public function runTag($tag){
		if ($tag =='')return ;
		if (isset($this->hook_tags[$tag]))
			$this->loadHook($this->hook_tags[$tag]);
	}
	
	/**
	 * 启动钩子
	 * @access public
	 * @param string	$type  钩子类型
	 * @return void
	 */
	public function run($type='')
	{
		if(!empty($type))
		{
			switch($type)
			{
				case 'controller_start':
					$this->runControllerStart();
					break;
				case 'controller_end':
					$this->runControllerEnd();
					break;
				case 'action_start':
					$this->runActionStart();
					break;
				case 'action_end':
					$this->runActionEnd();
					break;
				default:
					$this->runControllerHook($type);
					break;
			}
		}
	}
	
	private function runControllerHook($type){
		if (isset($this->hooks[$type]))
			$this->loadHook($this->hooks[$type]);
	}
	
	/**
	 * 运行控制器实例化时需要调用的钩子
	 * @access private
	 * @return void
	 */
	private function runControllerStart()
	{
		if(isset($this->hooks['controller_start']))
			$this->loadHook($this->hooks['controller_start']);
	}

	/**
	 * 运行控制器实例化之后需要调用的钩子
	 * @access private
	 * @return void
	 */
	private function runControllerEnd()
	{
		if(isset($this->hooks['controller_end']))
			$this->loadHook($this->hooks['controller_end']);
	}

	/**
	 * 控制器方法(Action)调用之前需要调用的钩子
	 * @access private
	 * @return void
	 */
	private function runActionStart()
	{
		if(isset($this->hooks['action_start']))
			$this->loadHook($this->hooks['action_start']);
	}


	/**
	 * 运行控制器方法(Action)调用之后需要调用的钩子
	 * @access private
	 * @return void
	 */
	private function runActionEnd()
	{
		if(isset($this->hooks['action_end']))
			$this->loadHook($this->hooks['action_end']);
	}

	/**
	 * 执行钩子脚本
	 * @access private
	 * @param array	 $hook 钩子脚本数组[一维]
	 * @return void
	 */
	private function loadHook(Array $hooks)
	{

		//检查是否开启Hook 钩子
		if(!$this->flag)
			return;

		//检查是否存在多个需要触发的脚本
		if(isset($hooks[0])){
			for($i=0;$i<count($hooks);$i++)
			{
				$this->loadHookFile($hooks[$i]['filename']);
				$new = $hooks[$i]['class'];
				$func = $hooks[$i]['function'];
				$object = new $new;
				$object->$func($hooks[$i]['params']);
			}
		}
		else
		{
			$this->loadHookFile($hooks['filename']);
			$new = $hooks['class'];
			$func = $hooks['function'];
			$object = new $new;
			$object->$func($hooks['params']);
		}
	}


	/**
	 * 载入钩子文件
	 * @access private
	 * @param string $filename 文件名称
	 * @return void
	 */
	private function loadHookFile($filename)
	{
		$file = SYSTEM_DIR.'hooks/'.$filename;
		if(is_file($file))
			require_once $file;
		else
			message_error('Load File','无法加载指定的钩子执行文件！',$file);
		return;
	}
}
?>